package com.ynny.dao;

import com.ynny.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class BookDaoImpl implements BookDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public Book selectBookById(Integer bookId) {

        String sql = "select * from t_book where book_id=?";

        Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Book.class), bookId);

        return book;
    }

    @Override
    public int updateStock(Integer bookId) {

        String sql = "update t_book set stock=stock-1 where book_id=?";
        int i = jdbcTemplate.update(sql, bookId);
        return i;
    }
}
